<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <!-- Tell the browser to be responsive to screen width -->
    <meta content="width=device-width, initial-scale=1, maximum-scale=1, user-scalable=no" name="viewport">
    <link rel="stylesheet" href="../css/bootstrap/bootstrap.min.css">
    <!-- Font Awesome -->
    <link rel="stylesheet" href="../css/font-awesome/font-awesome.min.css">
    <!-- Ionicons -->
    <link rel="stylesheet" href="../css/ionicons/ionicons.min.css">
    <!-- Theme style -->
    <link rel="stylesheet" href="../css/AdminLTE/AdminLTE.min.css">
    <!-- AdminLTE Skins. We have chosen the skin-blue for this starter
          page. However, you can choose any other skin. Make sure you
          apply the skin class to the body tag so the changes take effect. -->
    <link rel="stylesheet" href="../css/skins/skin-blue.min.css">
    <link rel="stylesheet" href="../css/fileinput/fileinput.min.css">

    <!-- HTML5 Shim and Respond.js IE8 support of HTML5 elements and media queries -->
    <!-- WARNING: Respond.js doesn't work if you view the page via file:// -->
    <!--[if lt IE 9]>
    <script src="../js/plugins/browserhacks/html5shiv.min.js"></script>
    <script src="../js/plugins/browserhacks/respond.min.js"></script>
    <![endif]-->

    <!-- jQuery 3 -->
    <script src="../js/plugins/jquery/jquery.min.js"></script>
    <!-- Bootstrap 3.3.7 -->
    <script src="../js/plugins/bootstrap/bootstrap.min.js"></script>
    <!-- AdminLTE App -->
    <script src="../js/plugins/adminlte/adminlte.min.js"></script>

    <!-- 文件上传插件-->
    <script src="../js/plugins/fileinput/fileinput.min.js"></script>
    <script src="../js/plugins/fileinput/zh.js"></script>
    <script src="../js/plugins/fileinput/theme.js"></script>

</head>
<body ng-app="myApp" ng-controller="fileController">
<div class="container-fluid">
    <form class="form-horizontal">
        <div class="form-group">
            <label for="location" class="col-sm-2 control-label text-right">选择文件</label>
            <div class="col-sm-8">
                <div class="input-group">
                    <input id='location' class="form-control"  onclick="$('#i-file').click();">
                    <label class="input-group-btn">
                        <input type="button" id="i-check" value="浏览文件" class="btn btn-primary"
                               onclick="$('#i-file').click();">
                    </label>
                </div>
            </div>
            <input type="file" name="file" id='i-file' accept=".xls, .xlsx"    ng-model="fileName" onchange="angular.element(this).scope().readMyFile(this);"    style="display: none">
        </div>


        <input type="file" name="file" ng-model="test"  onchange="angular.element(this).scope().readMyFile(this);">
    </form>

</div>
<script src="../js/plugins/angularjs/angular.min.js"></script>
<script src="../js/plugins/xlsx/xlsx.core.min.js"></script>
<script>
    var app = angular.module("myApp", []);
    app.controller("fileController", function ($scope) {
        $scope.readMyFile=function(obj){
            var files = obj.files;

            var fileReader = new FileReader();
            fileReader.onload = function(ev) {
                try {
                    var data = ev.target.result,
                        workbook = XLSX.read(data, {
                            type: 'binary'
                        }), // 以二进制流方式读取得到整份excel表格对象
                        persons = []; // 存储获取到的数据
                } catch (e) {
                    console.log('文件类型不正确');
                    return;
                }

                // 表格的表格范围，可用于判断表头是否数量是否正确
                var fromTo = '';
                // 遍历每张表读取
                for (var sheet in workbook.Sheets) {
                    if (workbook.Sheets.hasOwnProperty(sheet)) {
                        fromTo = workbook.Sheets[sheet]['!ref'];
                        console.log(fromTo);
                        persons = persons.concat(XLSX.utils.sheet_to_json(workbook.Sheets[sheet]));
                        // break; // 如果只取第一张表，就取消注释这行
                    }
                }

                console.log(persons);
            };

            // 以二进制方式打开文件
            fileReader.readAsBinaryString(obj.files[0]);
        }
    });

    $('#excel-file').change(function(e) {
        var files = e.target.files;

        var fileReader = new FileReader();
        fileReader.onload = function(ev) {
            try {
                var data = ev.target.result,
                    workbook = XLSX.read(data, {
                        type: 'binary'
                    }), // 以二进制流方式读取得到整份excel表格对象
                    persons = []; // 存储获取到的数据
            } catch (e) {
                console.log('文件类型不正确');
                return;
            }

            // 表格的表格范围，可用于判断表头是否数量是否正确
            var fromTo = '';
            // 遍历每张表读取
            for (var sheet in workbook.Sheets) {
                if (workbook.Sheets.hasOwnProperty(sheet)) {
                    fromTo = workbook.Sheets[sheet]['!ref'];
                    console.log(fromTo);
                    persons = persons.concat(XLSX.utils.sheet_to_json(workbook.Sheets[sheet]));
                    // break; // 如果只取第一张表，就取消注释这行
                }
            }

            console.log(persons);
        };

        // 以二进制方式打开文件
        fileReader.readAsBinaryString(files[0]);
    });
</script>
</body>
</html>